package com.example.agriculturalmanagement;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.agriculturalmanagement.adapter.DataInfoAdapter;
import com.example.agriculturalmanagement.databinding.ActivityDataInfoBinding;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class DataInfoActivity extends AppCompatActivity {
    private ActivityDataInfoBinding binding;

    private String mName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mName = getIntent().getStringExtra("name");
        binding = ActivityDataInfoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setSupportActionBar(binding.toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle(getString(R.string.app_name));
        }
        BmobQuery<Person> query = new BmobQuery<Person>();
        query.addWhereEqualTo("name", mName);
        //返回50条数据，如果不加上这条语句，默认返回10条数据
        query.setLimit(50);
        //执行查询方法
        query.findObjects(new FindListener<Person>() {
            @Override
            public void done(List<Person> object, BmobException e) {
                if (e == null) {
                    if (object == null || object.size() == 0) {
                        return;
                    }
                    Person person = object.get(0);
                    if (person.isShowTemperature()){
                       binding.llLayout1.setVisibility(View.VISIBLE);
                       binding.line1.setVisibility(View.VISIBLE);
                    }else {
                        binding.llLayout1.setVisibility(View.GONE);
                        binding.line1.setVisibility(View.GONE);
                    }
                    if (person.isShowHumidity()){
                        binding.llLayout2.setVisibility(View.VISIBLE);
                        binding.line2.setVisibility(View.VISIBLE);
                    }else {
                        binding.llLayout2.setVisibility(View.GONE);
                        binding.line2.setVisibility(View.GONE);
                    }
                    if (person.isShowSoilMoisture()){
                        binding.llLayout3.setVisibility(View.VISIBLE);
                        binding.line3.setVisibility(View.VISIBLE);
                    }else {
                        binding.llLayout3.setVisibility(View.GONE);
                        binding.line3.setVisibility(View.GONE);
                    }
                    if (person.isShowConcentration()){
                        binding.llLayout4.setVisibility(View.VISIBLE);
                        binding.line4.setVisibility(View.VISIBLE);
                    }else {
                        binding.llLayout4.setVisibility(View.GONE);
                        binding.line4.setVisibility(View.GONE);
                    }
                    if (person.isShowLight()){
                        binding.llLayout5.setVisibility(View.VISIBLE);
                    }else {
                        binding.llLayout5.setVisibility(View.GONE);
                    }

                    binding.tvValue1.setText(person.getTemperature() + "℃");
                    binding.tvValue2.setText(person.getHumidity() + "%");
                    binding.tvValue3.setText(person.getSoilMoisture() + "%");
                    binding.tvValue4.setText(person.getConcentration() + "ppm");
                    binding.tvValue5.setText(person.getLight() + "lx");

                } else {
                    Log.i("bmob", "失败：" + e.getMessage() + "," + e.getErrorCode());
                }
            }
        });

        binding.toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}